﻿module Sustainalytics.Shell.Admin {

    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;

    var mod = angular.module(shellAdminModuleName);

    mod.controller('RootController',
        ['$scope', 'RoutingService', '$location', '$rootScope', 'staticObjectsAdmin', 'notificationFactory', 'AuthenticationService',
            ($scope, RoutingService, $location, $rootScope, staticObjects: Common.Admin.IStaticObjects, notificationFactory: Common.INotificationFactory, AuthenticationService: Sustainalytics.Authentication.IAuthenticationService) => {

                //$scope.userInfo = $rootScope.userInfo;
                //$rootScope.$watch('userInfo', function () {
                //    $scope.userInfo = $rootScope.userInfo;
                //});

                $scope.$on('$routeChangeSuccess',(e, current, previous) => {
                    $scope.activeViewPath = $location.path();

                });

                $scope.isActive = (path) => {
                    if ($location.path().substr(0, path.length) == path) {
                        if (path == "/" && $location.path() == "/") {
                            return true;
                        }
                        else {
                            if (path == "/") {
                                return false;
                            }
                        }
                        return true;
                    }
                    else {
                        return false;
                    }
                };

                $scope.logout = () => {
                    AuthenticationService.logout();
                };

                var errorCallback = (data, status, headers, config) => {
                    $scope.loading = false;
                    if (data != "") {
                        notificationFactory.error(data.ExceptionMessage, data.message);
                    }
                    else {
                        notificationFactory.error(status, staticObjects.getErrorMesageByStatusCode(status));
                    }
                };
            }]);
}